package book;

public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;

    public BookList(){
        books[0] = new Book("三国演义","罗贯中",50,"小说");
        books[1] = new Book("三国演","罗贯",20,"小说");
        books[2] = new Book("三国","罗",5,"小说");
        this.usedSize = 3;
    }

    public Book[] getBook() {
        return books;
    }

    public Book getBooks(int pos) {
        return books[pos];
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }

    public void addBook(Book book , int currensize) {
        books[currensize] = book;
        this.usedSize++;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public boolean isFull(){
        if (this.usedSize == books.length){
            return false;
        }
        return true;
    }

    public int getBookId(String bookName){
        for (int i = 0; i < this.usedSize; i++) {
            if (bookName.equals(books[i].getName())){
                return i;
            }
        }
        return -1;
    }

    public void deleBookById(int ID,Book[] book){
        for (int i = ID; i < this.usedSize; i++) {
            books[i] = books[i + 1];
        }
        book[usedSize] = null;
        this.usedSize--;
    }

    public int BorrowedBook(String bookName){
        for (int i = 0; i < this.usedSize; i++) {
            if (books[i].getName().equals(bookName)) {
                if (books[i].isLend()) {
                    return -1;
                } else{
                    books[i].setLend(true);
                    return 1;
                }
            }
        }
        return 0;
    }

    public int returnBook(String bookName){
        int flag = 0;
        for (int i = 0; i < this.usedSize; i++) {
            if(bookName.equals(books[i].getName()) && books[i].isLend()){
                books[i].setLend(false);
                flag = 1;
                break;
            } else if (!books[i].isLend()) {
                flag = -1;
            }
        }
        return flag;
    }

}
